// pages/tixian/tixian.js
const app = getApp();
// 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: [
      {
        'name':'微信',
        'PayWay':2,

      },

      ],
    index: '0',
    fangshi: [
      {
        'id': '1',
        'img': 'https://weiqing.yiyawlkj.com/xhyg/img/wxhui.png',
        'seleced_img': 'https://weiqing.yiyawlkj.com/xhyg/img/wxhong.png'
      },
      {
        'id': '2',
        'img': 'https://weiqing.yiyawlkj.com/xhyg/img/yhkhong.png',
        'seleced_img': 'https://weiqing.yiyawlkj.com/xhyg/img/yhkhui.png'
      }
    ],
    fangshi1: [],
    hostUrl: app.globalData.hostUrl,
    file: [],
    hidden_value: true,
    button: 'true',
    uploaderList: [], //已选择的图片临时路径数组
    uploaderNum: 0, //已选择图片个数
    showUpload: true, //用来判断是否可继续选择图片,当传至9张时不可继续上传
    type:'银行卡'
  },
  // 支付宝、微信
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value); //index为数组点击确定后选择的item索引
    this.setData({
      index: e.detail.value,
      type: this.data.array[e.detail.value]
    })
  },

  // 删除图片
  clearImg: function (e) {
    var nowList = []; //新数据
    var uploaderList = this.data.uploaderList; //原数据

    for (let i = 0; i < uploaderList.length; i++) {
      if (i == e.currentTarget.dataset.index) {
        continue;
      } else {
        nowList.push(uploaderList[i])
      }
    }
    this.setData({
      uploaderNum: this.data.uploaderNum - 1,
      uploaderList: nowList,
      showUpload: true
    })
  },
  //展示图片
  showImg: function (e) {
    var that = this;
    wx.previewImage({
      urls: that.data.uploaderList,
      current: that.data.uploaderList[e.currentTarget.dataset.index]
    })
  },
  //上传图片
  upload: function (e) {
    var that = this;
    wx.chooseImage({
      // count: 9 - that.data.uploaderNum, // 默认9
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let tempFilePaths = res.tempFilePaths;
        // console.log(tempFilePaths);return ;
        let uploaderList = that.data.uploaderList.concat(tempFilePaths);
        // if (uploaderList.length == 9) {
        //   that.setData({
        //     showUpload: false
        //   })
        // }
        that.setData({
          uploaderList: uploaderList,
          uploaderNum: uploaderList.length,
        })
        // for (var i = 0; i < tempFilePaths.length; i++) {
          // console.log(tempFilePaths[i]);
          that.upload_server(tempFilePaths[0]);
          // file.push(res.tempFilePaths[i]);
        // }

      }
    })
  },

  //上传服务器
  upload_server: function (data) {
    var that = this;
    
    wx.uploadFile({
      url: app.globalData.apiUrl + 'Shop/upimg', // 仅为示例，非真实的接口地址
      filePath: data,
      name: 'file',
      formData: {
        user: 'test'
      },
      success(res) {
        if (res.data !== "") {
          // var back_push=that.data.file;
          // console.log()
          // that.data.file.push(res.data)
          that.setData({
            file: res.data,
          })
        }
        console.log(that.data.file, '返回图片长度')
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(wx.getStorageSync(userInfo))
    var that = this;
    var L = [];
    L = that.data.fangshi;
    that.setData({
      fangshi1: L,
      type: 1,
    });
    var userId = wx.getStorageSync('userId');
    that.system();//获取系统参数
    that.getUser(userId);//读取会员
    // console.log(that.data.shouruhe, '余额')
  },
  getUser: function (userId) {//读取会员
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'user/getUser',
      data: {
        // opt: 'getUser',
        userId: userId,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data != null) {
          that.setData({
            islock: res.data.IsLock,
            user: res.data,
          })

        }
      }

    })
    // console.log(that.data.canIUse)
  },
  //获取系统参数
  system: function () {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'User/getsystem',
      success: function (res) {

        that.setData({
          system: res.data,
        })
        console.log(that.data.system['Id'], '系统参数')
      }
    })
  },
  //提现提交
  formSubmit: function (e) {
    var that = this;
    var formData = e.detail.value;
    console.log(that.data.file)
    // if (that.data.uploaderList==""){
    //   wx.showToast({
    //     title: '凭证必填',
    //     icon: 'none',
    //   })
    //   return;
    // }
    // if (that.data.file == "") {
    //   wx.showToast({
    //     title: '图片未上传完成',
    //     icon: 'none',
    //   })
    //   return;
    // }
    // formData['img'] = that.data.file;
    formData['MemberId'] = wx.getStorageSync('userId');
    formData['MemberCode'] = that.data.user.Code;
    formData['NickName'] = that.data.user.NickName;
    formData['type'] =2;
    if (wx.getStorageSync('userId') !== "") {
      if (formData['Amount'] == ""){
        wx.showToast({
          title: '金额必填',
          icon: 'none',
        })
        return;
      }
      that.setData({
        hidden: false,
      })
      if (that.data.button == true) {
        //添加充值
        wx.request({
          url: app.globalData.apiUrl + 'User/remit_add',
          
          data: formData,
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            // console.log(res)
            if (res.data.status == 1) {
              that.setData({
                hidden_value: false,
              });
              wx.showToast({
                title: res.data.message,
                icon: 'none',
                duration: 1500,
                success: function () {
                  that.pay(res.data.orderid);
                }
              })

            } else {
              wx.showToast({
                title: res.data.message,
                icon: 'none',
                duration: 1500,
                success: function () {
                  that.setData({
                    button: 'true',
                  })
                }
              })
            }

          }
        })
      }
    } else {
      wx.showModal({
        title: '提示',
        content: '请先登录',
        success: function () {
          wx.switchTab({
            url: '/pages/login/login',
          })
        }
      })
    }
  },
  pay: function (id) {//支付
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Pay/remit_do',
      data: {
        // opt: 'GetUnifiedOrderResult',
        userId: wx.getStorageSync('userId'),
        id: id,//订单id
        // typeId: 2,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res, '支付返回信息')
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          'paySign': res.data.paySign,
          'success': function (res) {
            wx.showToast({
              title: '支付成功',
            })
            wx.navigateTo({
              url: '../topup_list/topup_list',
            })
          },
          'fail': function (res) {
            wx.showToast({
              title: '支付失败',
            })
            wx.navigateTo({
              url: '../topup_list/topup_list',
            })
          },
          'complete': function (res) {
            wx.navigateTo({
              url: '../topup_list/topup_list',
            })
          }
        })

      }
    })
  },
  gh_img: function (e) {
    var that = this;
    var fangshi = that.data.fangshi;
    var r = e.currentTarget.dataset.id;
    var L = [];
    var n = '';
    var m = '';
    for (var i in fangshi) {
      if (fangshi[i]['id'] == r) {
        n = fangshi[i]['img'];
        fangshi[i]['img'] = fangshi[i]['seleced_img'];
        fangshi[i]['seleced_img'] = n;
      }
      if (fangshi[i]['id'] != r) {
        m = fangshi[i]['img'];
        fangshi[i]['img'] = fangshi[i]['seleced_img'];
        fangshi[i]['seleced_img'] = m;
      }
      L.push(fangshi[i]);
    }
    that.setData({
      fangshi1: L,
      tixiantype: r,
    });
  },
  //全部提现
  quanbutixian: function () {
    console.log(11)
    var that = this;
    that.setData({
      amount: that.data.shouruhe,
    });
  },
  getamount: function (e) {
    console.log(e)
    var that = this;
    that.setData({
      amount: e.detail.value,
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      button: true
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */

})